import {reqGetCode,reqUserRegister,reqUserLogin,reqUserInfo} from '../../api'
//登录与注册的模块
const state = {
    code:"",
    token:'',
    userInfo:''
};
const mutations = {
    GETCODE(state,code){
        state.code = code
    },
    USERLOGIN(state,token){
        state.token = token
    },
    GETSERINFO(state,userInfo){
        state.userInfo = userInfo
    }
};
const actions = {
    //获取验证码
    async getCode({commit},phone){
      let result = await reqGetCode(phone);
    if(result.code==200){
        commit("GETCODE",result.data)
        return 'ok'
    }else{
        return Promise.reject(new Error('faile'))
    }
    },
    //用户注册
    async userRegister({commit},user){
         let result = await reqUserRegister(user)
        if(result.code==200){
            return 'ok'
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 用户登录 
    async userLogin({commit},data){
        let result =  await reqUserLogin(data)
        if(result.code ==200) {
            commit("USERLOGIN",result.data.token);
            return 'ok'
        }else{
            return Promise.reject(new Error("faile"));
        }
    },
    //获取用户信息
    async getUserInfo(){
       let result = await reqUserInfo()
       if(result.code ==200){
        commit('GETSERINFO',result.data.token);
        localStorage.setItem("TOKEN",result.data.token)
        return "ok"
       }    
    }
};
const getters = {};
export default{
    state,
    mutations,
    actions,
    getters
}